﻿using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace CPPEI.Coolzon.UI
{
    public class ViewContainer : Grid
    {
        private List<PopViewDecorator> _popViews;
        private UI.View _currentView;
        private List<UI.View> _history;

        public UI.View CurrentView
        {
            get
            {
                return _currentView;
            }
        }
        
        public ViewContainer()
        {
            _popViews = new List<PopViewDecorator>();
            _history = new List<View>();
        }

        /// <summary>
        /// 切换视图
        /// </summary>
        /// <param name="view"></param>
        public void SwitchView(View view)
        {
            if (_history != null)
            {
                for (int index = 0; index < _history.Count; index++)
                {
                    _history[index].Deactive();
                }
                _history.Clear();
            }

            if (_currentView != null)
            {
                this.Children.Remove(_currentView);
                _currentView.Deactive();
            }
            this.Children.Add(view);
            _currentView = view;

        }

        /// <summary>
        /// 切换出子视图
        /// </summary>
        /// <param name="subView"></param>
        public void SwitchSubView(View subView)
        {
            if (_currentView == null)
            {
                return;
            }
            _history.Add(_currentView);

            this.Children.Remove(_currentView);
            //_currentView.Deactive();
            this.Children.Add(subView);
            _currentView = subView;
        }

        /// <summary>
        /// 返回到上一级视图，通过SwitchSubView打开之后，可以调用这个
        /// </summary>
        public void Back()
        {
            if (_history.Count > 0)
            {
                this.Children.Remove(_currentView);
                _currentView.Deactive();
                this.Children.Add(_history.Last());
                _currentView = _history.Last();
            }
        }

        public void Remove(View view)
        {
            if (_currentView == view)
            {
                this.Children.Remove(_currentView);
                _currentView.Deactive();
                _currentView = null;
            }
        }

        /// <summary>
        /// 弹出视图
        /// </summary>
        /// <param name="view"></param>
        public void PopView(View view)
        {
            PopViewDecorator popView = new PopViewDecorator(view);
            popView.Close += popView_Close;
            popView.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            popView.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _popViews.Add(popView);
            this.Children.Add(popView);
        }

        public void PopView(string title, View view)
        {
            PopViewDecorator popView = new PopViewDecorator(view);
            popView.SetTitle(title);
            popView.Close += popView_Close;
            popView.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            popView.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _popViews.Add(popView);
            this.Children.Add(popView);
        }

        /// <summary>
        /// 显示弹出视图，并且可以设置是否显示左上角的关闭按钮
        /// </summary>
        /// <param name="title"></param>
        /// <param name="view"></param>
        /// <param name="allowClose">true：显示关闭按钮，false：不显示关闭按钮</param>
        public void PopView(string title, View view, bool allowClose)
        {
            PopViewDecorator popView = new PopViewDecorator(view);
            popView.SetTitle(title);
            popView.ShowCloseButton(allowClose);
            popView.Close += popView_Close;
            popView.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            popView.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _popViews.Add(popView);
            this.Children.Add(popView);
        }

        void popView_Close(View view, bool isCancel)
        {
            _popViews.Remove((PopViewDecorator)view);
            this.Children.Remove(view);
        }

        #region dlg
        
        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="view"></param>
        /// <param name="allowClose"></param>
        public void PopViewInDialog(string title, View view, bool allowClose)
        {
            DialogViewDecorator popView = new DialogViewDecorator(view);
            popView.SetTitle(title);
            popView.ShowCloseButton(allowClose);
            popView.ShowDialog();
        }

        #endregion
    }
}
